﻿--[[
   @brief    lanuch game animation
   @author   rickonzhang
   @version  1.0
   @date     2015-03-28
--]]

--[[ below is copied from as code 
    /**
     * 游戏启动界面数据描述。
     * 需求：游戏启动要消耗一段的时间，需提供一个界面告知用户游戏正在拉起，同时需告知用户TGP正在为用户提供的一些服务；
     *     0、告知用户游戏正在启动；
     *     1、告知用户游戏运行环境是否安全；
     *     2、告知用户加速信息；
     *         2.(1)、加速关闭时:"当前网络延迟"+ping值；
     *         2.(2)、加速优时:"已开启智能加速"+ping值；
     *         2.(3)、非加速优时:"已开启智能加速"；
     *     3、启动进度需控制播放时间2S；
     *     4、整个窗口最大生命期5S；
     *     5、游戏图标显示和游戏名称；
     * 
     * 设计概要：
     * 1、提供游戏图标和游戏名称设置接口；
     * 2、提供安全信息设置接口；
     * 3、提供加速开关状态设置接口（该数据需从设置中心拿数据）；
     * 4、提供加速信息设置接口（该数据需从加速中心拿数据）；
     * 5、需添加一个计时器用于控制该窗口生命周期；
     * 
     * @author Archer
     */

     C_MAIN_SHOW_LAUNCH_GAME_UI
     C_MAIN_HIDE_LAUNCH_GAME_UI
--]]
lga_view = {
    --properity -----------------
    pDialog = nil;
    dwRootId = 0;
    dwNetDelayId = 0;

    pProgressCtrl = nil;    --进度条
    pStateCtrl = nil;       --状态文本
    pLaunchGameCtrl = nil;  --无检测启动动画
    pCheckGameCtrl = nil;   --检测启动动画
    pSkipCtrl = nil;        --跳过
    pCheckOKCtrl = nil;     --检测结果安全图标
    pRepairCtrl = nil;      --修复中图标

    progress_increase_step = 5; --20次，100ms，2s跑完动画,每次步进5
    timers_for_display = 10;        --单位秒 

    pHealthTips = nil;      -- 健康公告提示

    nShowTime = 0;          -- 显示时间

    -----------------------------
    init = function()
        comm_center.listen_broadcast("Svr_CloudGameState", 
                    lga_view.change_cloud_game_state);
    end;

    delay_hide = function()
        -- 最少显示10秒钟
        if (os.time() - lga_view.nShowTime < lga_view.timers_for_display) then
            if lga_view.bTmrStart then
                return;
            end;
        
            local tpfui = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= tpfui then
                tpfui:AddInterval("lga_view.on_timer", 1000);
                lga_view.bTmrStart = true;
                base_utility.log("[lga_view]Delay hide, start timer.", true);
                return;
            end;
        end;

        base_utility.log("[lga_view]Delay hide, hide view.", true);
        lga_view.hide();
    end;

    on_timer = function()
        if (os.time() - lga_view.nShowTime < lga_view.timers_for_display) then
            base_utility.log("[lga_view]on_timer, waiting for close.");
            return;
        end;

        local tpfui = base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= tpfui then      
            tpfui:RemoveInterval("lga_view.on_timer");
            lga_view.bTmrStart = false;
        end;

        base_utility.log("[lga_view]on_timer, hide view.", true);
        lga_view.hide();
    end;

    hide = function()
        base_utility.log("[lga_view]hide dialog");
        lga_view.stop_animation();

        if nil ~= lga_view.pDialog then
            lga_view.pDialog:Show(FALSE);
        end;
    end;

    show = function(bshow, is_repair, is_rail, show_tips, is_cloud_game)
        -- body
        if lga_view.pDialog == nil then
            lga_view.create_dialog();
        end

        if lga_view.pDialog == nil then
            base_utility.log("[lga_view] show error!", true);
            return;
        end
        -- 显示健康公告
        lga_view.ShowHealthTips(show_tips);
        if bshow then
            is_repair = banner_control.get_check_version_flag();
            lga_view.nShowTime = os.time();
            if lga_view.pDialog:IsVisible() == TRUE then
                lga_view.pDialog:BringToTop();
            else
                lga_view.pDialog:Show(TRUE);
                if not is_cloud_game and not is_repair then
                    lga_view.start_animation();
                end
            end;
            if not is_cloud_game then
                if is_repair then
                    base_utility.log("[lga_view] need repair game");
                    lga_view.switch_to_repair_mode(is_rail);
                else
                    base_utility.log("[lga_view] not need repair game");
                    if not is_rail then
                        lga_view.switch_to_launch_mode();
                    else
                        lga_view.switch_to_launch_rail_mode();
                    end
                end
            else
                base_utility.log("[lga_view] launch cloud game mode");
                lga_view.switch_to_launch_cloud_game_mode();
            end
        else
            lga_view.pDialog:Show(FALSE);
            lga_view.stop_animation();

            base_utility.log("[lga_view]show, false.");
            lga_model.game_id = 0;
            lga_view.nShowTime = 0;
        end

        local game_base_info = banner_model.get_game_base_info(lga_model.game_id);
        if game_base_info ~= nil and game_base_info.launch_pop_display_time ~= nil then
            local display_time = game_base_info.launch_pop_display_time;
            if display_time ~= 0 then
                lga_view.timers_for_display = display_time;
                base_utility.log("[lga_view]set display time"..lga_view.timers_for_display);
            end;
        end;

        local value ={};
        if bshow ~= nil then 
            value.bshow = tostring(bshow);
        else
            value.bshow = "null";
        end;

        if is_repair ~= nil then 
            value.is_repair = tostring(is_repair);
        else
            value.is_repair = "null";
        end;

        if is_rail ~= nil then 
            value.is_anti = tostring(is_rail);
        else
            value.is_anti = "null";
        end;

        if value ~= nil then
            base_utility.mainframe_report("lanuch_game_view", "show", base_utility.table_to_json(value));
        else
            base_utility.mainframe_report("lanuch_game_view", "show", "");
        end;
    end;

    --无检测启动
    switch_to_launch_mode = function()
        lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr(ID_LOGIN_GAME_TISP_1));
        lga_view.safe_start_animation(1);
        lga_view.safe_show_ctrl(lga_view.pSkipCtrl, FALSE);
        lga_view.show_state_icon(0);
     end;

    switch_to_launch_rail_mode = function()
        --lga_view.show_state_icon(0);
        lga_view.progress_increase_step = 5;
        lga_view.timers_for_display = 10;
        lga_view.start_animation();
        lga_view.safe_start_animation(2);
        lga_view.safe_show_ctrl(lga_view.pSkipBtnCtrl, FALSE);
        lga_view.safe_set_ctrl_text(lga_view.pTitleCtrl, LoadStr(ID_LOGIN_GAME_TISP_2));
        lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr(ID_LOGIN_GAME_TISP_3));
        lga_view.safe_set_ctrl_text(lga_view.pSubtitleCtrl, "");
    end;

    switch_to_launch_cloud_game_mode = function()
        lga_view.set_progress(0);
        lga_view.safe_set_ctrl_text(lga_view.pTitleCtrl, LoadStr(ID_LOGIN_GAME_TISP_2));
        lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr("FRAME_BOTTOM_CLOUD_GAME_LAUNCHING"));
        lga_view.safe_start_animation(1);
        lga_view.safe_show_ctrl(lga_view.pSkipCtrl, FALSE);
        lga_view.show_state_icon(0);
    end;

    start_animation = function ()
        lga_control.start_timer();
        lga_view.set_progress(50);
        base_utility.log("[lga_view]start_animation");
    end;

    stop_animation = function ()
        lga_control.stop_timer();

        if nil ~= lga_view.pProgressCtrl then
            lga_view.pProgressCtrl:SetPos(0, TRUE);
        end;

        if nil ~= lga_view.pDialog then
            lga_view.pDialog:Show(FALSE);
        end;

        lga_view.nShowTime = 0;
        base_utility.log("[lga_view]stop_animation");
    end;

    time_100ms_tick = function ()
        base_utility.log("timer tick, timers_for_display:"..lga_view.timers_for_display);
        lga_view.pProgressCtrl:OffsetPos(lga_view.progress_increase_step, TRUE);

        if (os.time() - lga_view.nShowTime > lga_view.timers_for_display) then -- 达到10s时间
            lga_view.pDialog:Show(FALSE);
            lga_view.stop_animation();
            lga_view.nShowTime = 0;
            base_utility.log("[lga_view]time_100ms_tick close.");
        end
    end;

    --修复检测启动
    switch_to_repair_mode = function(is_rail)
        if is_rail then
            lga_view.progress_increase_step = 5;
            lga_view.timers_for_display = 10;
            lga_view.start_animation();
        end;
        lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr(ID_LOGIN_GAME_TISP_4));
        lga_view.safe_start_animation(2);

        lga_view.safe_show_ctrl(lga_view.pSkipCtrl, FALSE);
        lga_view.show_state_icon(0);
    end;

    -- 检查文件进度
    on_game_repair_process = function(gameid, ischeck_step, progress, status)
        base_utility.log("gameis is "..gameid.." progress is "..progress.."status is "..status);
        if ischeck_step == true then
            lga_view.safe_show_ctrl(lga_view.pSkipCtrl, TRUE);
        elseif ischeck_step == false then
            lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr(ID_LOGIN_GAME_TISP_5));
            lga_view.safe_start_animation(0);
            lga_view.safe_show_ctrl(lga_view.pSkipCtrl, FALSE);
            lga_view.show_state_icon(2);
        end;
        lga_view.set_progress(progress);
    end;

    -- 检查修复文件结果
    on_game_repair_result = function(gameid, ischeck_step, result)
        base_utility.log("gameis is "..gameid.."result is "..result);
        lga_view.safe_start_animation(0);
        lga_view.safe_show_ctrl(lga_view.pSkipCtrl, FALSE);
        if result == 0 then
            if ischeck_step == true then
                lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr(ID_LOGIN_GAME_TISP_6));
                lga_view.show_state_icon(1);
            else
                lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr(ID_LOGIN_GAME_TISP_7));
                lga_view.show_state_icon(2);
            end;
            return;
        end;
        --检查修复失败，直接启动游戏
        lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, LoadStr(ID_LOGIN_GAME_TISP_8));
        lga_view.show_state_icon(1);
        lga_view.set_progress(100);
    end;

    -- set current check progress percent
    set_progress = function(value)
        if lga_view.pProgressCtrl ~= nil then
            lga_view.pProgressCtrl:SetPos(value,TRUE);
        end
    end;

    -- enable/disable skip button
    enable_skip_button = function(value)
        if lga_view.pSkipBtnCtrl ~= nil then
            lga_view.pSkipBtnCtrl:Enable(value);
        end
    end;

    -- 跳过当前步骤
    skip_this_step = function(uilib,dwID_Click)
        lga_view.safe_show_ctrl(lga_view.pSkipBtnCtrl, FALSE);
        lga_control.skip_this_step();
        lga_view.show(false);
    end;


    -- 0:hide all;1:show ok;2:show warning
    show_state_icon = function(state)
        if state == 1 then
            lga_view.safe_show_ctrl(lga_view.pCheckOKCtrl, TRUE);
            lga_view.safe_show_ctrl(lga_view.pRepairCtrl, FALSE);
        elseif state == 2 then
            lga_view.safe_show_ctrl(lga_view.pCheckOKCtrl, FALSE);
            lga_view.safe_show_ctrl(lga_view.pRepairCtrl, TRUE);
        else
            lga_view.safe_show_ctrl(lga_view.pCheckOKCtrl, FALSE);
            lga_view.safe_show_ctrl(lga_view.pRepairCtrl, FALSE);
        end
    end;

    safe_show_ctrl = function(ctrl, flag)
        if ctrl ~= nil then
            ctrl:Show(flag);
        end
    end;

    safe_set_ctrl_text = function(ctrl, u8_txt)
        local const_ui_str, str_holder = U8toCUIStr(u8_txt);
        if ctrl ~= nil then
            local s_ctrl = tolua.cast(ctrl, "Tenio::ITStatic");
            if s_ctrl ~= nil then
                s_ctrl:SetText(const_ui_str);
            end
        end        
    end;  

    --state : 0：全部动画隐藏，1：显示启动动画，2:显示检测动画
    safe_start_animation = function(state)
        if state == 0 then 
            lga_view.safe_show_ctrl(lga_view.pLaunchGameCtrl, FALSE);
            lga_view.safe_show_ctrl(lga_view.pCheckGameCtrl, FALSE);
        elseif state == 1 then
            lga_view.safe_show_ctrl(lga_view.pLaunchGameCtrl, TRUE);
            lga_view.safe_show_ctrl(lga_view.pCheckGameCtrl, FALSE);
        elseif state == 2 then
            lga_view.safe_show_ctrl(lga_view.pLaunchGameCtrl, FALSE);
            lga_view.safe_show_ctrl(lga_view.pCheckGameCtrl, TRUE);
        else
            lga_view.safe_show_ctrl(lga_view.pLaunchGameCtrl, FALSE);
            lga_view.safe_show_ctrl(lga_view.pCheckGameCtrl, FALSE);
        end      
    end; 

    create_dialog = function ()
        if lga_view.pDialog == nil then
            lga_view.dwRootId = layout_mgr.restore_layout("LanuchGameTip.tpf");
            if lga_view.dwRootId ~= 0 then
                local main_uilib = ui_utility.uilib();
                lga_view.pDialog = tolua.cast(main_uilib:GetControlPtr(lga_view.dwRootId), "Tenio::ITDialog");

                local const_str, str_holder = U8toCUIStr("Level_Progress");
                local id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pProgressCtrl = tolua.cast(main_uilib:GetControlPtr(id), "Tenio::ITLevel");

                const_str, str_holder = U8toCUIStr("TxtState_stc");
                id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pStateCtrl = main_uilib:GetControlPtr(id);
                
                const_str, str_holder = U8toCUIStr("LaunchGame_miv");
                id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pLaunchGameCtrl = main_uilib:GetControlPtr(id);

                const_str, str_holder = U8toCUIStr("CheckGame_miv");
                id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pCheckGameCtrl = main_uilib:GetControlPtr(id);

                const_str, str_holder = U8toCUIStr("Skip_rtx");
                id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pSkipCtrl = main_uilib:GetControlPtr(id);

                const_str, str_holder = U8toCUIStr("CheckOK_ctn");
                id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pCheckOKCtrl = main_uilib:GetControlPtr(id);

                const_str, str_holder = U8toCUIStr("RepairGame_ctn");
                id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pRepairCtrl = main_uilib:GetControlPtr(id);

                const_str, str_holder = U8toCUIStr("TinyStatic_Delay");
                lga_view.dwNetDelayId = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);

                -- 健康公告提示
                const_str, str_holder = U8toCUIStr("rtbHealthTips");
                id = main_uilib:GetControlIDbyPath(const_str, lga_view.dwRootId);
                lga_view.pHealthTips = main_uilib:GetControlPtr(id);
            else
                base_utility.log("create lanuch game dialog error!!!!!", true);
            end
        end
    end;  

    -- 改变大小
    ShowHealthTips = function(bShow)
        if nil == lga_view.pDialog or nil == lga_view.pHealthTips then
            TGPLog.LogE("HealthTips", "Show tips, control is null.");
            return;
        end;

        lga_view.SetLaunchBackground(bShow);
        if (bShow) then
            lga_view.pHealthTips:Show(TRUE);
            local   rtTemp = lga_view.pDialog:GetRectEx(FALSE,TRUE);
            rtTemp.right  = rtTemp.left + 763;
            rtTemp.bottom = rtTemp.top  + 349;  
            lga_view.pDialog:MoveEx(rtTemp,TRUE);
        else
            lga_view.pHealthTips:Show(FALSE);
            local   rtTemp = lga_view.pDialog:GetRectEx(FALSE,TRUE);
            rtTemp.right  = rtTemp.left + 718;
            rtTemp.bottom = rtTemp.top  + 202;
            lga_view.pDialog:MoveEx(rtTemp,TRUE);
        end;
    end;

    SetLaunchBackground = function(show_htips)
        base_utility.log("SetLaunchBackground:".. lga_model.game_id);
        local game_base_info = banner_model.get_game_base_info(lga_model.game_id);
        local launch_bg_url = "ONEIMAGE:client_popup_wnd\\launch_bg_default_s.png";
        if show_htips then
            launch_bg_url = "ONEIMAGE:client_popup_wnd\\launch_bg_default.png";
        end;
        if game_base_info and game_base_info.launch_bg_url ~= nil and 
                game_base_info.launch_bg_url ~='' then
                launch_bg_url = string.format(
                    "%s|%s;Stretch;null;",
                    launch_bg_url, game_base_info.launch_bg_url);
        end;
        ui_utility.setproperty_by_id(lga_view.dwRootId, "Background", launch_bg_url);
    end;

    bTmrStart = false;

    change_cloud_game_state = function(param)
        local cur_state = param.state;
        local show_text =  nil;
        local state_normal = false;
        --  只处理更新下载显示
        if cur_state == CloudGameState.kCloudGameStateUpdating then
            if param.progress <= 0 then
                show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_CHECKING");
            else
                show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_DOWNLOADING");
                show_text = show_text .. param.progress .. "%";
            end;
        elseif cur_state == CloudGameState.kCloudGameStateLaunching then
            show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_LAUNCHING");
        elseif cur_state == CloudGameState.kCloudGameStateRunning then
            show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_RUNNING");
            state_normal = true;
        elseif cur_state == CloudGameState.kCloudGameStateReady 
            or cur_state == CloudGameState.kCloudGameStateNeedUpdate 
            or cur_state == CloudGameState.kCloudGameStateUpdateFailed
            or cur_state == CloudGameState.kCloudGameStateLaunchFailed then
            show_text = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_LAUNCH_CLOUD_GAME");
            state_normal = true;
        end;
        local number_game_id = tonumber(param.game_id);
        if number_game_id == 0 or number_game_id == nil then
            return;
        end
       if number_game_id == tonumber(lga_model.game_id) and
            show_text ~=nil then
            if state_normal == true then
               base_utility.log("change_cloud_game_state show text2:" .. show_text);
               lga_view.safe_set_ctrl_text(lga_view.pTitleCtrl, LoadStr(ID_LOGIN_GAME_TISP_2));
               lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, show_text);
               if (os.time() - lga_view.nShowTime < 2) then
                   local tpfui =
                    base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");
                    tpfui:AddInterval("lga_view.delay_hide_when_cloud_game_no_update", 1000);
               else
                   lga_view.show(false);
               end;
            else
                base_utility.log("change_cloud_game_state show text:" .. show_text);
                lga_view.safe_set_ctrl_text(lga_view.pTitleCtrl, LoadStr(ID_LOGIN_GAME_TISP_2));
                lga_view.safe_set_ctrl_text(lga_view.pStateCtrl, show_text);
                if param.progress ~=nil and param.progress > 0 then
                    lga_view.set_progress(tonumber(param.progress));
                end;
            end;
        end;
    end;

    delay_hide_when_cloud_game_no_update = function ()
        lga_view.show(false);

        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval("lga_view.delay_hide_when_cloud_game_no_update");
    end
};

lga_view.init();